import {
  list
} from '../../api/blog'


Page({

  data: {
    list: [],
    form: {
      pageNum: 1,
      pageSize: 20,
      title: '',
      month: '',
      orderType: 1
    },
    title: '',
    reachButtom: false
  },

  cancel() {
    wx.navigateBack()
  },
  doSearch() {
    this.data.form.title = this.data.title
    if (!this.data.form.title && !this.data.form.month) {
      return false
    }

    // 每次搜索重置第一页
    this.data.form.pageNum = 1
    this.data.form.month = ''
    this.setData({
      form: this.data.form
    })
    list(this.data.form).then(res => {
      this.setData({
        list: res.records
      })
    })
  },

  toDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/detail/index?id=${id}`,
    })
  },

  handleButtom() {
    if (this.data.reachButtom) {
      return false;
    }
    this.data.form.pageNum = this.data.form.pageNum + 1
    this.setData({
      form: this.data.form
    })
    list(this.data.form).then(res => {
      this.setData({
        list: this.data.list.concat(res.records)
      })
      // wx.hideLoading()

      if (res.total === this.data.list.length) {
        this.setData({
          reachButtom: true
        })
      }
    })
  },
  onLoad(options) {
    this.data.form.month = options.month
    this.setData({
      form: this.data.form
    })
    list(this.data.form).then(res => {
      this.setData({
        list: res.records
      })
    })
  },
})